package com.hesc.minio.controller;

import com.google.gson.Gson;
import com.hesc.minio.util.ContentTypeUtil;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class FileServer {

    @Resource
    private MinioClient minioClient;

    @Value("${filetype.soft}")
    String softType;

    @PostMapping(value = "/addfile")
    public String addFile(MultipartFile file) {
        System.out.println(file.toString());
        Map<String, Object> result = new HashMap<>();
        try {
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("picurl").build());
            if (!found) {
                log.error("bucket is not exist");
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket("picurl")
                        .build());
            }

            ObjectWriteResponse objectWriteResponse = minioClient.putObject(
                    PutObjectArgs.builder().bucket("picurl").object("test.png").stream(
                            file.getInputStream(), file.getSize(), -1)
                            .build());
            GetObjectResponse url = minioClient.getObject(GetObjectArgs.builder().bucket("picurl").object("test.png").build());    //文件访问路径
            result.put("code", "1");
            result.put("msg", "onSuccess");
            result.put("obj", objectWriteResponse);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "0");
            result.put("msg", e.getMessage());
        }
        return new Gson().toJson(result);
    }

    @GetMapping(value = "/getUrl")
    public String getUrl(@RequestParam String bucket, @RequestParam String objectname) {
        String url = null;
        try {
            url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket(bucket)
                            .object(objectname)
                            .expiry(24 * 60 * 60)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(url);

        System.out.println("####:" + softType);

        ContentTypeUtil contentTypeUtil = new ContentTypeUtil();
        System.out.println("@@@@@@:" + contentTypeUtil.resolveSuffix("png"));


        return url;
    }

    @PostMapping(value = "/addUrlFile")
    public String addUrlFile(String urlFile) {
        // http://172.16.33.36:18080/200x/http://172.16.33.36:19000/picurl/test.png
        Map<String, Object> result = new HashMap<>();
        try {
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("picurl").build());
            if (!found) {
                log.error("bucket is not exist");
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket("thumpic")
                        .build());
            }

            URL url = null;
            InputStream is = null;
            try {
                url = new URL(urlFile);
                //利用HttpURLConnection对象,我们可以从网络中获取网页数据.
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.connect();
                //得到网络返回的输入流
                is = conn.getInputStream();
                ObjectWriteResponse objectWriteResponse = minioClient.putObject(
                        PutObjectArgs.builder().bucket("thumpic").object("thumpic_1.png").stream(
                                is, -1, 5 * 1024 * 1024)
                                .build());
                result.put("code", "1");
                result.put("msg", "onSuccess");
                result.put("obj", objectWriteResponse);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    is.close();
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "0");
            result.put("msg", e.getMessage());
        }
        return new Gson().toJson(result);
    }
}
